package com.github.ecbp.user.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.ecbp.common.constant.ConstantCode;
import com.github.ecbp.common.data.service.impl.BaseServiceImpl;
import com.github.ecbp.user.service.entity.TAdmin;
import com.github.ecbp.user.service.entity.TAdminRoleRelation;
import com.github.ecbp.user.service.mapper.AdminRoleRelationMapper;
import com.github.ecbp.user.service.service.AdminRoleRelationService;
import com.github.ecbp.user.service.vo.AdminRoleIdVo;
import com.github.ecbp.user.service.vo.ListInfoVo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class AdminRoleRelationServiceImpl extends BaseServiceImpl<AdminRoleRelationMapper, TAdminRoleRelation> implements AdminRoleRelationService {

    @Override
    public List<ListInfoVo> pageInfoList(List<Long> ids) {
        QueryWrapper<TAdminRoleRelation> wrapper = new QueryWrapper<>();
        wrapper.select("distinct ar.role_id id");
        wrapper.in("ar.role_id", ids);
        wrapper.eq("ar.delete_status", ConstantCode.TRUE);
        wrapper.eq("a.delete_status", ConstantCode.TRUE);
        wrapper.eq("a.status", ConstantCode.TRUE);
        wrapper.groupBy("ar.role_id");
        return baseMapper.pageInfoList(wrapper);
    }

    @Override
    public List<AdminRoleIdVo> getRoleIdByAdminId(Long adminId) {
        QueryWrapper<TAdminRoleRelation> wrapper = new QueryWrapper<>();
        wrapper.select("distinct r.id roleId");
        wrapper.eq("admin_id", adminId);
        wrapper.eq("r.status", ConstantCode.TRUE);
        wrapper.eq("r.delete_status", ConstantCode.TRUE);
        wrapper.eq("ar.delete_status", ConstantCode.TRUE);
        return baseMapper.getRoleIdByAdminId(wrapper);
    }

    @Override
    public List<AdminRoleIdVo> getRoleIdByAdminId(Set<Long> adminIds) {
        QueryWrapper<TAdminRoleRelation> wrapper = new QueryWrapper<>();
        wrapper.select("distinct r.id roleId", "r.name roleName", "ar.admin_id adminId");
        wrapper.in("admin_id", adminIds);
        wrapper.eq("r.status", ConstantCode.TRUE);
        wrapper.eq("r.delete_status", ConstantCode.TRUE);
        wrapper.eq("ar.delete_status", ConstantCode.TRUE);
        return baseMapper.getRoleIdByAdminId(wrapper);
    }

    @Override
    public List<TAdmin> getAdminIdByRoleId(Long roleId) {
        QueryWrapper<TAdminRoleRelation> wrapper = new QueryWrapper<>();
        wrapper.select("distinct a.id", "a.login_phone");
        wrapper.eq("ar.role_id", roleId);
        wrapper.eq("a.status", ConstantCode.TRUE);
        wrapper.eq("a.delete_status", ConstantCode.TRUE);
        wrapper.eq("ar.delete_status", ConstantCode.TRUE);
        return baseMapper.getAdminIdByRoleId(wrapper);
    }

    @Override
    public List<TAdmin> getAdminIdByRoleId(Set<Long> roleIds) {
        QueryWrapper<TAdminRoleRelation> wrapper = new QueryWrapper<>();
        wrapper.select("distinct a.id", "a.login_phone");
        wrapper.in("ar.role_id", roleIds);
        wrapper.eq("a.status", ConstantCode.TRUE);
        wrapper.eq("a.delete_status", ConstantCode.TRUE);
        wrapper.eq("ar.delete_status", ConstantCode.TRUE);
        return baseMapper.getAdminIdByRoleId(wrapper);
    }
}
